package com.lzp.customview

import android.os.Bundle
import android.os.Handler
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.base.BaseActivity
import com.view.loadmore.LoadMoreRecyclerView
import kotlinx.android.synthetic.main.load_more_activity.*

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/7/10
 */
class LoadMoreRvActivity : BaseActivity() {

    private val TAG = "LoadMoreRvActivity"
    private val loadMoreAdapter = CommentLoadMoreAdapter(0)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.load_more_activity)
    }

    override fun initView() {
        super.initView()
        loadMoreRv.layoutManager = LinearLayoutManager(this)

        loadMoreRv.setAdapter(loadMoreAdapter)
        getNetData()
        loadMoreRv.setOnLoadmoreListener(object : LoadMoreRecyclerView.OnLoadMoreListener {
            override fun onLoadMore() {
                Log.e(TAG, "onLoadMore: " )
                getNetData()
            }
        })
    }

    private fun getNetData() {
        if (loadMoreAdapter.mGetItemCount()>30){
            loadMoreRv.onFail()
            return
        }
        Handler().postDelayed({
            loadMoreRv.stopLoad()
            if(loadMoreAdapter.mGetItemCount()>30){
                loadMoreRv.onComplete()
                return@postDelayed
            }
            loadMoreAdapter.loadMore()
        },1000)
    }
}